﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using ComicIsland.Common;
using ComicIsland.Common.Entities.LocalJson;
using ComicIsland.Common.StorageHelper;

/**
 * 创建时间：2015/1/18 11:46:17
 * 作者：yong.blackcore
 * Email：itblackhole@outlook.com
 * QQ：50404503
 */
namespace ComicIsland.App.CIHelper
{
    public class LocalHistorySearchWordHelper
    {
        public string FilePath { get; private set; }
        public LocalHistorySearchWordHelper()
        {
            this.FilePath = "ComicIsland_FilePath_LocalHistorySearchWord";
        }

        /// <summary>
        /// 添加搜索词至历史表
        /// </summary>
        /// <param name="searchwordInfo"></param>
        /// <returns></returns>
        public bool Add(LocalHistorySearchWord searchwordInfo)
        {
            bool result = false;
            try
            {
                if (!(searchwordInfo != null && !string.IsNullOrWhiteSpace(searchwordInfo.SearchKeyword)))
                {
                    LogHelper.LogError("搜索词为空！", this.GetType(), MethodBase.GetCurrentMethod());
                    return false;
                }
                //
                ObservableCollection<LocalHistorySearchWord> items = null;
                if (IsolatedStorageHelper.Instance.FileExists(this.FilePath))
                {
                    string json = IsolatedStorageHelper.Instance.ReadText(this.FilePath);
                    if (!string.IsNullOrWhiteSpace(json))
                    {
                        items = JSONHelper.Deserialize<ObservableCollection<LocalHistorySearchWord>>(json);
                    }
                }
                //
                if (items != null && items.Count > 0)
                {
                    var item = (from p in items where p.SearchKeyword.Equals(searchwordInfo.SearchKeyword) select p).FirstOrDefault<LocalHistorySearchWord>();
                    if (item == null)
                    {
                        items.Add(searchwordInfo);
                    }
                }
                else
                {
                    items = new ObservableCollection<LocalHistorySearchWord>();
                    items.Add(searchwordInfo);
                }
                //
                string jsons = JSONHelper.Serialize<ObservableCollection<LocalHistorySearchWord>>(items);
                if (!string.IsNullOrWhiteSpace(jsons))
                {
                    IsolatedStorageHelper.Instance.WriteText(jsons, this.FilePath, true);
                    result = true;
                }
            }
            catch { }
            return result;
        }

        public bool Update(LocalHistorySearchWord searchwordInfo)
        {
            bool result = false;
            try
            {
                if (!(searchwordInfo != null && !string.IsNullOrWhiteSpace(searchwordInfo.SearchKeyword)))
                {
                    LogHelper.LogError("搜索词为空！", this.GetType(), MethodBase.GetCurrentMethod());
                    return false;
                }
                //
                ObservableCollection<LocalHistorySearchWord> items = null;
                if (IsolatedStorageHelper.Instance.FileExists(this.FilePath))
                {
                    string json = IsolatedStorageHelper.Instance.ReadText(this.FilePath);
                    if (!string.IsNullOrWhiteSpace(json))
                    {
                        items = JSONHelper.Deserialize<ObservableCollection<LocalHistorySearchWord>>(json);
                    }
                }
                //
                if (items != null && items.Count > 0)
                {
                    var item = (from p in items where p.SearchKeyword.Equals(searchwordInfo.SearchKeyword) select p).FirstOrDefault<LocalHistorySearchWord>();
                    if (item != null)
                    {
                        item.SearchKeyword = searchwordInfo.SearchKeyword;
                        string jsons = JSONHelper.Serialize<ObservableCollection<LocalHistorySearchWord>>(items);
                        if (!string.IsNullOrWhiteSpace(jsons))
                        {
                            IsolatedStorageHelper.Instance.WriteText(jsons, this.FilePath, true);
                            result = true;
                        }
                    }
                }
            }
            catch { }
            return result;
        }

        /// <summary>
        /// 漫画是否存在书架中
        /// </summary>
        /// <param name="searchword"></param>        
        /// <returns></returns>
        public bool IsExist(string searchword)
        {
            bool result = false;
            try
            {
                ObservableCollection<LocalHistorySearchWord> items = null;
                if (IsolatedStorageHelper.Instance.FileExists(this.FilePath))
                {
                    string json = IsolatedStorageHelper.Instance.ReadText(this.FilePath);
                    if (!string.IsNullOrWhiteSpace(json))
                    {
                        items = JSONHelper.Deserialize<ObservableCollection<LocalHistorySearchWord>>(json);
                    }
                }
                //
                if (items != null && items.Count > 0)
                {
                    var item = (from p in items where p.SearchKeyword.Equals(searchword) select p).FirstOrDefault<LocalHistorySearchWord>();
                    if (item != null)
                    {
                        result = true;
                    }
                }
            }
            catch { }
            finally { }
            return result;
        }


        public ObservableCollection<LocalHistorySearchWord> GetAll()
        {
            ObservableCollection<LocalHistorySearchWord> result = null;
            try
            {
                if (IsolatedStorageHelper.Instance.FileExists(this.FilePath))
                {
                    string json = IsolatedStorageHelper.Instance.ReadText(this.FilePath);
                    if (!string.IsNullOrWhiteSpace(json))
                    {
                        result = JSONHelper.Deserialize<ObservableCollection<LocalHistorySearchWord>>(json);
                    }
                }
            }
            catch { }
            return result;
        }

        /// <summary>
        /// 清除搜索历史
        /// </summary>
        /// <returns></returns>
        public bool Remove()
        {
            bool result = false;
            try
            {
                if (IsolatedStorageHelper.Instance.FileExists(this.FilePath))
                {
                    IsolatedStorageHelper.Instance.DeleteFile(this.FilePath);
                }
            }
            catch { }
            return result;
        }
    }
}
